package com.frost.trans.controller;

import com.frost.trans.model.UserInfo;
import com.frost.trans.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager; // 事务管理器
    @Autowired
    private TransactionDefinition transactionDefinition; // 事务的属性

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/registry")
    public String registry(String userName, String password) {
        // 获取当前事务
        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);

        Integer result = userInfoService.insertUser(userName, password);

        // 回滚事务
        dataSourceTransactionManager.rollback(transaction);
//        dataSourceTransactionManager.commit(transaction);
        return "" + result;
    }
}
